package org.d7_buffered_stream;

import java.io.*;

/**
 * 目标：观察原始流和缓冲流的性能
 */
public class TimeTest4 {
    //复制视频的路径
    private static final String SRC_FILE = "D:\\test.mp4";
    //复制到哪个目的地
    private static final String DEST_FILE = "D:\\idea\\ bbb\\";
    public static void main(String[] args) {
            copy1();
            copy2();
            copy3();
            copy4();

    }

    public static void copy1(){
        long startTime = System.currentTimeMillis();
        try(
                //使用低级字节流按照一个一个字节的形式复制文件
                InputStream is = new FileInputStream(SRC_FILE);
                OutputStream os = new FileOutputStream(DEST_FILE+ "1.mp4");
        ) {
            int c ;
            while ((c = is.read() )!= -1){
                os.write(c);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
       long endTime = System.currentTimeMillis();
        System.out.println("低级字节流一个一个字节复制耗时：" + (endTime - startTime)/1000.0 +"s" );

    }

    public static void copy2(){
        long startTime = System.currentTimeMillis();
        try(
                //使用低级字节流按照一个一个字节的形式复制文件
                InputStream is = new FileInputStream(SRC_FILE);
                OutputStream os = new FileOutputStream(DEST_FILE+ "2.mp4");
        ) {
            byte[] buffer = new byte[1024]; // 1kb
            int len ;
            while ((len = is.read(buffer) )!= -1){
                os.write(buffer,0,len);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        long endTime = System.currentTimeMillis();
        System.out.println("低级字节流按照字节数组的形式复制耗时：" + (endTime - startTime)/1000.0 +"s" );

    }

    public static void copy3(){
        long startTime = System.currentTimeMillis();
        try(
                InputStream is = new FileInputStream(SRC_FILE);
                BufferedInputStream bis = new BufferedInputStream(is);
                OutputStream os = new FileOutputStream(DEST_FILE+ "3.mp4");
                BufferedOutputStream bos = new BufferedOutputStream(os);
        ) {
            int c ;
            while ((c = bis.read() )!= -1){
                bos.write(c);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        long endTime = System.currentTimeMillis();
        System.out.println("高级缓冲字节流一个一个字节复制耗时：" + (endTime - startTime)/1000.0 +"s" );

    }
    public static void copy4(){
        long startTime = System.currentTimeMillis();
        try(
                InputStream is = new FileInputStream(SRC_FILE);
                BufferedInputStream bis = new BufferedInputStream(is);
                OutputStream os = new FileOutputStream(DEST_FILE+ "4.mp4");
                BufferedOutputStream bos = new BufferedOutputStream(os);
        ) {
            byte[] buffer = new byte[1024]; // 1kb
            int len ;
            while ((len = bis.read(buffer) )!= -1){
                bos.write(buffer,0,len);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        long endTime = System.currentTimeMillis();
        System.out.println("高级字节流按照字节数组的形式复制耗时：" + (endTime - startTime)/1000.0 +"s" );

    }


}
